<template>
	<div class="purchase">
		<back-header>确认订单</back-header>
		<div class="purchase-container">
			<default-address v-if="defaultAddress" :address="defaultAddress"/>
			<new-address v-else/>
			<purchase-list :purchaseList="purchase.list"/>
			<purchase-note v-model="remark"/>
			<choose-coupons v-if="false"/>
			<purchase-price
					:total="purchaseTotal"
					:isNeedLogistics="isNeedLogistics"
					:amountLogistics="amountLogistics"
			/>
		</div>
		<Footer :total="amountTotle" @click="submitPurchase" :defaultAddress="defaultAddress"/>
	</div>
</template>

<script>
	import UserAddress from '@/services/userAddress';
	import Order from '@/services/order';
	import {mapState, mapGetters, mapActions} from 'vuex';
	import deepClone from '@/utils/deepClone';
	import backHeader from '#/Public/backHeader';
	import newAddress from '#/Purchase/newAddress';
	import defaultAddress from '#/Purchase/defaultAddress';
	import purchaseList from '#/Purchase/purchaseList';
	import purchaseNote from '#/Purchase/purchaseNote';
	import chooseCoupons from '#/Purchase/chooseCoupons';
	import purchasePrice from '#/Purchase/purchasePrice';
	import Footer from '#/Purchase/Footer';

	const _userAddress = new UserAddress();
	const _order = new Order();
	export default {
		name: "Purchase",
		created() {
			this.getDefaultAddress();
			this.getPrice();
		},
		data() {
			return {
				defaultAddress: null,
				remark: '',
				amountLogistics: '',
				amountTotle: '',
				goodsNumber: '',
				isNeedLogistics: false,
				score: '',
				from: ''
			}
		},
		computed: {
			...mapState(['purchase', 'user']),
			...mapGetters(['purchaseTotal']),
		},
		beforeRouteEnter(to, from, next) {
			next(vm => {
				vm.from = from.name
			})
		},
		methods: {
			...mapActions(['clearPurchase', 'clearShopCart']),
			getDefaultAddress() {
				_userAddress.getDefaultAddress().then(res => {
					this.defaultAddress = res.data;
				}).catch(err => {
					this.$alert({
						title: '错误',
						message: '请先设置您的收货地址！'
					})
				});
			},
			submitPurchase() {
				if (!this.defaultAddress) {
					this.$alert({
						title: '错误',
						message: '请先设置您的收货地址！'
					});
					return;
				}
				let params = this.orderData(false);
				_order.createOrder(params).then(res => {
					this.resetPurchase();
					this.$router.push('/payment/' + res.data.id);
				}).catch(err => {
					this.$router.push('/');
				})
			},
			resetPurchase() {
				if (this.from === 'cart') {
					this.clearShopCart();
				}
				this.clearPurchase()
			},
			getPrice() {
				let params = this.orderData(true);
				_order.createOrder(params).then(res => {
					this.amountLogistics = res.data.amountLogistics;//	amountLogistics: 0 // 物流数量
					this.amountTotle = res.data.amountTotle;// 	amountTotle: 0.7  // 总价格
					this.goodsNumber = res.data.goodsNumber;// 	goodsNumber: 1  // 商品数量
					this.isNeedLogistics = res.data.isNeedLogistics;// 	isNeedLogistics: false // 是否需要物流
					this.score = res.data.score// 	score: 10  // 分数
				}).catch(err => {
					this.$router.push('/');
				})
			},
			orderData(calculate) {
				let token = this.user.token;
				let defaultAddress = deepClone(this.defaultAddress);
				let remark = this.remark;
				// let couponId = 0; // 优惠券信息
				let goodsJson = [];
				for (let item of this.purchase.list) {
					let obj = {
						goodsId: item.propertyChildIdsData ? item.propertyChildIdsData.goodsId : item.id,
						number: item.count,
						propertyChildIds: item.propertyChildIdsStr,
						logisticsType: 0
					};
					goodsJson.push(obj);
				}
				let goodsJsonStr = JSON.stringify(goodsJson);
				let expireMinutes = 0; // 不自动关闭订单
				// let kjid = 0; // 砍价id
				// let pingtuanOpenId = 0; // 拼团id
				let data = {
					token,
					...defaultAddress,
					remark,
					calculate,
					goodsJsonStr,
					expireMinutes
				};
				return data;
			}
		},
		components: {
			backHeader,
			newAddress,
			defaultAddress,
			purchaseList,
			purchaseNote,
			chooseCoupons,
			purchasePrice,
			Footer
		}
	}
</script>
